using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Security.Cryptography;

namespace Srvtools
{
    public static class PublicKey
    {
        public static string GetPublicKey(string userId, string dataBase, string solution)
        {
            int i1 = userId.Length;
            int i2 = dataBase.Length;
            int i3 = solution.Length;

            string s = userId + i1.ToString() + dataBase + i2.ToString() + solution + i3.ToString() +
                (i1 * i2 * i3).ToString() + (i1 + i2 + i3).ToString();

            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] pwdBytes = md5.ComputeHash(Encoding.ASCII.GetBytes(s));
            byte[] result = md5.ComputeHash(pwdBytes);
            string ss = BitConverter.ToString(result);

            return userId + "-" + dataBase + "-" + solution + "-" + ss.Replace("-", "");
        }

        public static string GetPublicKey2(string userId, string dataBase, string solution, int language)
        {
            int i1 = userId.Length;
            int i2 = dataBase.Length;
            int i3 = solution.Length;

            string s = userId + i1.ToString() + dataBase + i2.ToString() + solution + i3.ToString() +
                (i1 * i2 * i3).ToString() + (i1 + i2 + i3).ToString();

            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] pwdBytes = md5.ComputeHash(Encoding.ASCII.GetBytes(s));
            byte[] result = md5.ComputeHash(pwdBytes);
            string ss = BitConverter.ToString(result);

            return userId + "-" + dataBase + "-" + solution + "-" + language.ToString() + "-" + ss.Replace("-", "");
        }

        public static bool CheckPublicKey(string key)
        {
            string[] ss = key.Split("-".ToCharArray());
            if (ss.Length == 4)
            {
                string userId = ss[0];
                string dataBase = ss[1];
                string solution = ss[2];

                string s1 = GetPublicKey(userId, dataBase, solution);
                if (key.Equals(s1))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }

        public static bool CheckPublicKey2(string key)
        {
            string[] ss = key.Split("-".ToCharArray());
            if (ss.Length == 5)
            {
                string userId = ss[0];
                string dataBase = ss[1];
                string solution = ss[2];
                int language = int.Parse(ss[3]);

                string s1 = GetPublicKey2(userId, dataBase, solution, language);
                if (key.Equals(s1))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
    }
}
